﻿// JScript 文件
Ext.apply(Ext.form.VTypes, {
  password: function(val, field) {
    if (field.initialPassField) {
      var pwd = Ext.getCmp(field.initialPassField);
      return (val == pwd.getValue());
    }
    return true;
  },  
  passwordText: '两次密码不匹配'
});
Ext.onReady(function(){
    Ext.BLANK_IMAGE_URL="../js/lib/ext/resources/images/gray/s.gif";
    Ext.QuickTips.init();
    var simple = new Ext.form.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,
        bodyStyle:'padding:5px 5px 0',
        labelWidth: 55,
        width: 330,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel: 'Email',
                name: 'email',
                vtype:'email',
                vtypeText:'请输入一个有效的邮件地址',
                allowBlank:false,
                blankText:"请输入您的邮件地址"
            },{
                fieldLabel: '密码',
                name: 'password',
                inputType: 'password',
                allowBlank:false,
                blankText:"请输入正确的密码"
            }
        ]
    });
    simple.addButton({
            text: '登陆',
            tooltip:'登陆您的账号',
			handler: function(){
			    simple.getForm().submit({
			        url:'User.aspx?action=login',
			        waitMsg:'提交中..',
			        waitTitle:'登陆',
			        timeout:20,
			        
			        success:function(form,action){
			            var res = action.result;
			            if(res.success=="false"){
			                Ext.Msg.alert("登陆",res.describe);
			                return false;
			            }else{
			                Ext.Msg.alert("登陆",res.describe,function(){
			                    location.href=res.redirect;
			                });
			            }
			            return true;
			        },
			        failure:function(form,action){
			            Ext.Msg.alert("错误","非法登陆！");
			            return false;
			        }
			    });
            }
        });
    simple.addButton({
        text: '取消',
        tooltip:'取消登陆',
        handler:function(){
            simple.form.reset();
        }
    });
    var register = new Ext.form.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame:true,
        bodyStyle:'padding:5px 5px 0',
        labelWidth: 55,
        width: 330,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel:'用户名',
                name:'username',
                allowBlank:false,
                blankText:"请输入一个有效用户名"
            },{
                fieldLabel: '密码',
                name: 'password',
                id:"pass",
                inputType: 'password',
                allowBlank:false,
                blankText:"请输入正确的密码"
            },{
                fieldLabel: '重复密码',
                name: 'confirm',
                inputType: 'password',
                allowBlank:false,
                blankText:"请输入正确的密码",
                vtype: 'password',
                initialPassField: 'pass' // id of the initial password field
            },{
                fieldLabel: 'Email',
                name: 'email',
                vtype:'email',
                vtypeText:'请输入一个有效的邮件地址',
                allowBlank:false,
                blankText:"请输入您的邮件地址"
            }
        ]
    });
    register.addButton({
            text: '注册',
            tooltip:'注册一个新的账号',
			handler: function(){
			    register.getForm().submit({
			        url:'User.aspx?action=register',
			        waitMsg:'提交中..',
			        waitTitle:'注册帐户',
			        timeout:20,
			        success:function(form,action){
			            var res = action.result;
			            if(res.success=="false"){
			                Ext.Msg.alert("注册",res.describe);
			            }else{
			                Ext.Msg.alert("注册","注册成功，马上进入后台！",function(){
			                    location.href="index.html";
			                });
			            }
			            return false;
			        },
			        failure:function(form,action){
			            Ext.Msg.alert("错误","非法注册！");
			            return false;
			        }
			    });
            }
        });
    register.addButton({
        text: '取消',
        tooltip:'取消注册',
        handler:function(){
            register.form.reset();
        }
    });
    var window = new Ext.Window({
        title: '登陆框',
        width: 360,
        height:190,
        minWidth: 300,
        minHeight: 100,
        layout: 'card',
        activeItem:0,
        shadow:true,
        plain:true,
        bodyStyle:'padding:5px;',
        bufferResize:true,
        resizable:false,
        maximizable:true,
        //buttonAlign:'center',
        items: [simple,register],
        

        buttons: [{
            text: '切换到注册框',
            tooltip:'登陆或者注册一个新的账号',
			handler: function(){
			    if(this.text=="切换到注册框"){
			        window.setTitle("注册框");
			        this.setText("切换到登陆框");
                    window.setHeight(300);
                    window.getLayout().setActiveItem(1);
                }else{
                    window.setTitle("登陆框")
                    this.setText("切换到注册框");
                    window.setHeight(190);
                    window.getLayout().setActiveItem(0);
                }
                window.center();
            }
        }]
    });
    window.show();
});